//
//  LeverageTradeView.m
//  XTXOneToOne
//
//  Created by Canny on 2018/10/29.
//  Copyright © 2018年 WK. All rights reserved.
//

#import "LeverageTradeView.h"
#import "LeverageTradeButton.h"

@interface LeverageTradeView ()

@property (nonatomic,strong)UIButton * btnBug;
@property (nonatomic,strong)UIButton * btnSale;

@property (nonatomic,strong)UIButton *  buyOrsaleBtn;

@property (nonatomic,strong)UILabel * nowTicker;

@property (nonatomic,strong)UIButton *   btn_multipNum1;
@property (nonatomic,strong)UIButton *   btn_multipNum2;



@property (nonatomic,strong)UILabel * lab_quote_symbol;

@property (nonatomic,strong)UILabel * lab_trustMoney;//委托金额
@property (nonatomic,strong)UILabel * lab_guaranteeMoney;//

@end

@implementation LeverageTradeView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor whiteColor];
        [self createUI];
    }
    return self;
}
-(void)setLab:(UILabel *)lab withColor:(UIColor *)color andFont:(CGFloat)font andText:(NSString *)text andTextAlignment:(NSTextAlignment)textAlignment{
    lab.text=text;
    lab.textColor=color;
    lab.font=[UIFont systemFontOfSize:font];
    lab.textAlignment=textAlignment;
}


-(void)createUI{
    
    CGFloat T1 = 30 ,T2 = 225;
    LeverageTradeButton * title = [[LeverageTradeButton alloc]init];
    title.lab1.text=@"盘口";
    title.lab2.text=@"价格";
    title.lab3.text=@"数量";
    title.frame=CGRectMake(G_SCREENWIDTH/2.0, 18, G_SCREENWIDTH/2.0, 30);
    [self addSubview:title];
    
    /*
     * 卖
     */
    for (int i=5; i>0; i--) {
        LeverageTradeButton * btn = [[LeverageTradeButton alloc]init];
        btn.Type=LeverageTradeButtonType_Sale;
        btn.tag = 10001+(5-i);
//        btn.tag = 10000+i;
        btn.frame = CGRectMake(G_SCREENWIDTH/2.0, T1+i*30, G_SCREENWIDTH/2.0, 30);
        btn.lab1.text=[NSString stringWithFormat:@"%d",(6-i)];
        [btn addTarget:self action:@selector(clickLeverageTradeButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
    }
    
    
    /*
     * 买
     */
    for (int j=0; j<5; j++) {
        LeverageTradeButton * btn = [[LeverageTradeButton alloc]init];
        btn.tag = 20001+j;
        btn.Type=LeverageTradeButtonType_Buy;
        btn.frame = CGRectMake(G_SCREENWIDTH/2.0, T2+j*30, G_SCREENWIDTH/2.0, 30);
        btn.lab1.text=[NSString stringWithFormat:@"%d",(j+1)];
        [btn addTarget:self action:@selector(clickLeverageTradeButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
    }
    
    
    UILabel * nowTicker = [[UILabel alloc]init];
    [self addSubview:nowTicker];
    [self setLab:nowTicker withColor:RGB(3, 192, 135) andFont:15 andText:@"0" andTextAlignment:NSTextAlignmentLeft];
    nowTicker.frame=CGRectMake(G_SCREENWIDTH/2.0, 208, 100, 20);
    _nowTicker=nowTicker;
    
    [self createBtnBugAndBtnSale];
    

    UILabel * limitedPrice = [[UILabel alloc]init];
    [self addSubview:limitedPrice];
    [self setLab:limitedPrice withColor:RGB(139, 159, 173) andFont:12 andText:@"限价" andTextAlignment:NSTextAlignmentLeft];
    limitedPrice.frame=CGRectMake(15, 60, 50, 40);
    
    UIView * USTD_bgView = [[UIView alloc]init];
    USTD_bgView.layer.borderColor=RGB(140, 159, 173).CGColor;
    [USTD_bgView.layer setBorderWidth:1.0];
    USTD_bgView.frame=CGRectMake(15, 110, (G_SCREENWIDTH/2.0-15*2), 45);
    [self addSubview:USTD_bgView];
    
    
    UITextField * textField_Price = [[UITextField alloc]init];
    [USTD_bgView addSubview:textField_Price];
    [self setTextField:textField_Price WithPlaceholder:@"单价" and:UIKeyboardTypeDecimalPad];
    textField_Price.frame=CGRectMake(10, 0, 100, 45);
    _textField_Price = textField_Price;
    
    
    UILabel * USDT = [[UILabel alloc]init];
    [USTD_bgView addSubview:USDT];
    _lab_quote_symbol = USDT;
    [self setLab:USDT withColor:RGB(139, 159, 173) andFont:12 andText:@"USDT" andTextAlignment:NSTextAlignmentLeft];
    [USDT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(45));
        make.right.equalTo(USTD_bgView.mas_right).offset(-10);
        make.bottom.equalTo(USTD_bgView.mas_bottom).offset(0);

    }];
    
    
    
    UIView * BTC_bgView = [[UIView alloc]init];
    BTC_bgView.layer.borderColor=RGB(140, 159, 173).CGColor;
    [BTC_bgView.layer setBorderWidth:1.0];
    BTC_bgView.frame=CGRectMake(15, 180, (G_SCREENWIDTH/2.0-15*2), 45);
    [self addSubview:BTC_bgView];
    
    UITextField * textField_Num = [[UITextField alloc]init];
    [BTC_bgView addSubview:textField_Num];
    [self setTextField:textField_Num WithPlaceholder:@"数量" and:UIKeyboardTypeDecimalPad];
    textField_Num.frame=CGRectMake(10, 0, 100, 45);
    _textField_Num = textField_Num;
    
    UILabel * BTC = [[UILabel alloc]init];
    _lab_base_symbol = BTC;
    [BTC_bgView addSubview:BTC];
    [self setLab:BTC withColor:RGB(139, 159, 173) andFont:12 andText:@"BTC" andTextAlignment:NSTextAlignmentLeft];
    [BTC mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(45));
        make.bottom.equalTo(BTC_bgView.mas_bottom).offset(0);
        make.right.equalTo(BTC_bgView.mas_right).offset(-10);
    }];
    
    UILabel * trustMoney = [[UILabel alloc]init];
    [self addSubview:trustMoney];
    [self setLab:trustMoney withColor:RGB(139, 159, 173) andFont:12 andText:@"委托金额" andTextAlignment:NSTextAlignmentLeft];
    trustMoney.frame=CGRectMake(15, 250, 100, 20);
    
    //委托金额
    UILabel * lab_trustMoney = [[UILabel alloc]init];
    [self addSubview:lab_trustMoney];
    [self setLab:lab_trustMoney withColor:RGB(139, 159, 173) andFont:12 andText:@"0" andTextAlignment:NSTextAlignmentRight];
    lab_trustMoney.frame=CGRectMake(80, 250, (G_SCREENWIDTH/2.0-90), 20);
    _lab_trustMoney = lab_trustMoney;
    
    UIButton *  multipNumBtn1 = [[UIButton alloc]init];
    [self addSubview:multipNumBtn1];
    multipNumBtn1.frame = CGRectMake(15, 280, 70, 20);
    [multipNumBtn1 setTitleColor:RGB(4, 192, 135) forState:UIControlStateNormal];
    [multipNumBtn1 setTitle:@"10X" forState:UIControlStateNormal];
    multipNumBtn1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    multipNumBtn1.titleLabel.font = [UIFont systemFontOfSize: 12.0];
    [multipNumBtn1 addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
    [multipNumBtn1.layer setBorderWidth:1.0];
    multipNumBtn1.layer.borderColor=RGB(4, 192, 135).CGColor;
    _btn_multipNum1=multipNumBtn1;
    multipNumBtn1.tag = 9003;
    
    UIButton *  multipNumBtn2 = [[UIButton alloc]init];
    [self addSubview:multipNumBtn2];
    multipNumBtn2.frame = CGRectMake(15+80, 280, 70, 20);
    [multipNumBtn2 setTitleColor:RGB(140, 159, 173) forState:UIControlStateNormal];
    [multipNumBtn2 setTitle:@"20X" forState:UIControlStateNormal];
    multipNumBtn2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    multipNumBtn2.titleLabel.font = [UIFont systemFontOfSize: 12.0];
    [multipNumBtn2 addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
    [multipNumBtn2.layer setBorderWidth:1.0];
    multipNumBtn2.layer.borderColor=RGB(140, 159, 173).CGColor;
    multipNumBtn2.tag = 9004;
    _btn_multipNum2=multipNumBtn2;
    
    UILabel * guaranteeMoney = [[UILabel alloc]init];
    [self addSubview:guaranteeMoney];
    [self setLab:guaranteeMoney withColor:RGB(139, 159, 173) andFont:12 andText:@"所需保证金" andTextAlignment:NSTextAlignmentLeft];
    guaranteeMoney.frame=CGRectMake(15, 310, 100, 20);
    
    UILabel * lab_guaranteeMoney = [[UILabel alloc]init];
    [self addSubview:lab_guaranteeMoney];
    [self setLab:lab_guaranteeMoney withColor:RGB(139, 159, 173) andFont:12 andText:@"0" andTextAlignment:NSTextAlignmentRight];
    lab_guaranteeMoney.frame=CGRectMake(80, 310, G_SCREENWIDTH/2.0-90, 20);
    _lab_guaranteeMoney = lab_guaranteeMoney;
    
    UIButton *  buyOrsaleBtn = [[UIButton alloc]init];
    [self addSubview:buyOrsaleBtn];
    buyOrsaleBtn.frame = CGRectMake(15, 350, (G_SCREENWIDTH/2.0-15*2), 35);
    [buyOrsaleBtn setTitleColor:RGB(255, 255, 255) forState:UIControlStateNormal];
    buyOrsaleBtn.backgroundColor=RGB(4, 192, 135);
    [buyOrsaleBtn setTitle:@"买入(做多)BTC" forState:UIControlStateNormal];
    buyOrsaleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    buyOrsaleBtn.titleLabel.font = [UIFont systemFontOfSize: 12.0];
    [buyOrsaleBtn addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
    _buyOrsaleBtn = buyOrsaleBtn;
    buyOrsaleBtn.tag=9005;
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:243/255.0 alpha:1];
    [self addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(10));
        make.width.equalTo(@(G_SCREENWIDTH));
        make.left.equalTo(self.mas_left).offset(0);
        make.bottom.equalTo(self.mas_bottom).offset(0);
    }];
    
    _lever = 10;
    
    HomeBaseModel * HB = [HomeBaseModel sharedHomeBaseModel];
    HomeBaseTabCellModel * SM = HB.selectedModel;
    
    if (SM) {
        _lab_base_symbol.text = [NSString stringWithFormat:@"%@",SM.base_symbol];
        _lab_quote_symbol.text = [NSString stringWithFormat:@"%@",SM.quote_symbol];
        _lab_trustMoney.text = [NSString stringWithFormat:@"0 %@",SM.quote_symbol];
        _lab_guaranteeMoney.text = [NSString stringWithFormat:@"0 %@",SM.quote_symbol];
        
    }
}

-(void)createBtnBugAndBtnSale{
    
    _Type= LeverageTradeViewType_Buy;
    
    CGFloat BTN_W1 =(G_SCREENWIDTH/2.0-15*2-15)/2.0;
    
    UIButton * btnBug = [[UIButton alloc]init];
    [self addSubview:btnBug];
    btnBug.frame = CGRectMake(15, 25, BTN_W1, 35);
    [btnBug setTitleColor:RGB(140, 159, 173) forState:UIControlStateNormal];
    [btnBug setTitle:@"买入" forState:UIControlStateNormal];
    btnBug.tag=9001;
    btnBug.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    btnBug.titleLabel.font = [UIFont boldSystemFontOfSize: 15.0];
    [btnBug addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
    [btnBug.layer setBorderWidth:1.0];
    btnBug.layer.borderColor=RGB(140, 159, 173).CGColor;
    _btnBug = btnBug;
    
    UIButton * btnSale = [[UIButton alloc]init];
    [self addSubview:btnSale];
    btnSale.frame = CGRectMake(15+15+BTN_W1, 25, BTN_W1, 35);
    [btnSale setTitleColor:RGB(140, 159, 173) forState:UIControlStateNormal];
    [btnSale setTitle:@"卖出" forState:UIControlStateNormal];
    btnSale.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    btnSale.titleLabel.font = [UIFont boldSystemFontOfSize: 15.0];
    [btnSale addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
    [btnSale.layer setBorderWidth:1.0];
    btnSale.layer.borderColor=RGB(140, 159, 173).CGColor;
    _btnSale = btnSale;
    btnSale.tag=9002;
    
    [self btnSelected:btnBug];
    
}

-(void)removeLeverageTradeButtonListDataSource{
    LTradeModel * Zero = [[LTradeModel alloc]init];
    Zero.price=@"0";
    Zero.amount=@"0";
    for (int i=0;i<5;i++) {
        LeverageTradeButton * btn1 = [self viewWithTag:10001+i];
        LeverageTradeButton * btn2 = [self viewWithTag:20001+i];
        btn1.dataSource=Zero;
        btn2.dataSource=Zero;
    }
}



-(void)setDataSource:(LeverageTradeModel *)dataSource{
    _dataSource = dataSource;
    if (dataSource) {
        
        HomeBaseModel * HB = [HomeBaseModel sharedHomeBaseModel];
        HomeBaseTabCellModel * SM = HB.selectedModel;
        if (SM) {
            _lab_base_symbol.text = [NSString stringWithFormat:@"%@",SM.base_symbol];
            _lab_quote_symbol.text = [NSString stringWithFormat:@"%@",SM.quote_symbol];
            
            [_buyOrsaleBtn setTitle:[NSString stringWithFormat:@"买入(做多)%@",SM.base_symbol] forState:UIControlStateNormal];

            
        }

        LTradeModel * Zero = [[LTradeModel alloc]init];
        Zero.price=@"0";
        Zero.amount=@"0";

        
        
        
        
//        int i = 0 , j = 0;
        for (int i=0;i<5;i++) {
            LeverageTradeButton * btn1 = [self viewWithTag:10001+i];
            LeverageTradeButton * btn2 = [self viewWithTag:20001+i];
            if (dataSource.asks_Bids_Model.asks.count>i) {
                btn1.dataSource=dataSource.asks_Bids_Model.asks[i];
            }else{
                btn1.dataSource=Zero;
            }
            if (dataSource.asks_Bids_Model.bids.count>i) {
                btn2.dataSource=dataSource.asks_Bids_Model.bids[i];
            }else{
                btn2.dataSource=Zero;
            }

        }
//        for (LTradeModel * model in dataSource.asks_Bids_Model.asks) {
//            LeverageTradeButton * btn = [self viewWithTag:10001+i];
//            btn.dataSource=model;
//            i++;
//        }
//
//        for (LTradeModel * model in dataSource.asks_Bids_Model.bids) {
//            LeverageTradeButton * btn = [self viewWithTag:20001+j];
//            btn.dataSource=model;
//            j++;
//        }
        if (dataSource.tickerModel.price.length>0) {
            _nowTicker.text = [NSString stringWithFormat:@"%@",dataSource.tickerModel.price];
            NSLog(@"dataSource.tickerModel.price-------%@",dataSource.tickerModel.price);
        }
    }
}

-(void)btnSelected:(UIButton *)btn{
    
    
    UIColor * green = RGB(4, 192, 135);
    UIColor * red = RGB(231, 109, 66);
    UIColor * gray = RGB(139, 159, 173);
    
    if (btn.tag == 9001) {

        _lever = 10;
        
        _Type = LeverageTradeViewType_Buy;

        
        [_btnBug setTitleColor:green forState:UIControlStateNormal];
        _btnBug.layer.borderColor=green.CGColor;
        
        [_btnSale setTitleColor:gray forState:UIControlStateNormal];
        _btnSale.layer.borderColor=gray.CGColor;
        
        [_buyOrsaleBtn setTitle:@"买入(做多)BTC" forState:UIControlStateNormal];
        _buyOrsaleBtn.backgroundColor=green;
        
        _btn_multipNum1.layer.borderColor=green.CGColor;
        [_btn_multipNum1 setTitleColor:green forState:UIControlStateNormal];

        _btn_multipNum2.layer.borderColor=gray.CGColor;
        [_btn_multipNum2 setTitleColor:gray forState:UIControlStateNormal];
        
    }else if (btn.tag == 9002){

        _lever = 10;

        
        _Type = LeverageTradeViewType_Sale;
        
        [_btnBug setTitleColor:gray forState:UIControlStateNormal];
        _btnBug.layer.borderColor=gray.CGColor;
        
        [_btnSale setTitleColor:red forState:UIControlStateNormal];
        _btnSale.layer.borderColor=red.CGColor;
        
        [_buyOrsaleBtn setTitle:@"卖出(做空)BTC" forState:UIControlStateNormal];
        _buyOrsaleBtn.backgroundColor=red;
        
        _btn_multipNum1.layer.borderColor=red.CGColor;
        [_btn_multipNum1 setTitleColor:red forState:UIControlStateNormal];
        
        _btn_multipNum2.layer.borderColor=gray.CGColor;
        [_btn_multipNum2 setTitleColor:gray forState:UIControlStateNormal];
        
    }else if (btn.tag == 9003){
        //  10X
        _lever = 10;

        if (_Type == LeverageTradeViewType_Buy) {
            _btn_multipNum1.layer.borderColor=green.CGColor;
            [_btn_multipNum1 setTitleColor:green forState:UIControlStateNormal];
        }else{
            _btn_multipNum1.layer.borderColor=red.CGColor;
            [_btn_multipNum1 setTitleColor:red forState:UIControlStateNormal];
        }
        _btn_multipNum2.layer.borderColor=gray.CGColor;
        [_btn_multipNum2 setTitleColor:gray forState:UIControlStateNormal];
        
    }else if (btn.tag == 9004){
        
        _lever = 20;
        
        _btn_multipNum1.layer.borderColor=gray.CGColor;
        [_btn_multipNum1 setTitleColor:gray forState:UIControlStateNormal];
        //  20X
        if (_Type == LeverageTradeViewType_Buy) {
            _btn_multipNum2.layer.borderColor=green.CGColor;
            [_btn_multipNum2 setTitleColor:green forState:UIControlStateNormal];
        }else{
            _btn_multipNum2.layer.borderColor=red.CGColor;
            [_btn_multipNum2 setTitleColor:red forState:UIControlStateNormal];
        }
        
    }else if (btn.tag == 9005){
        if (_textField_Num.text.length<1) {
            [HUD showErrorMessage:@"请输入数量！"];
            return;
        }
        if (_textField_Price.text.length<1) {
            [HUD showErrorMessage:@"请输入价格！"];
            return;
        }
        
        if (self.block) {
            self.block(_Type);
        }
    }
}

-(void)clickLeverageTradeButton:(LeverageTradeButton*)btn{
    
    [btn click];
    
//    NSLog(@"btn.lab2----%@----",btn.lab2.text);
    _textField_Price.text = btn.lab2.text;
    [self caculateGG];
}



-(void)setTextField:(UITextField*)textField WithPlaceholder:(NSString *)placeholder and:(UIKeyboardType)type  {
    
    
    textField.placeholder=placeholder;
    textField.borderStyle = UITextBorderStyleNone;
//    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
//    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    textField.backgroundColor=[UIColor clearColor];
    textField.font = [UIFont systemFontOfSize:15];
    textField.textColor = RGB(32, 64, 89);
    textField.textAlignment = NSTextAlignmentLeft;
    //    textField.returnKeyType = UIReturnKeyDefault;
    textField.keyboardType = type;
    [textField setValue:RGB(190, 190, 190) forKeyPath:@"_placeholderLabel.textColor"];
    [textField setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:(UIControlEventEditingChanged)];
    textField.inputAccessoryView = [self addToolbar];

}

- (UIToolbar *)addToolbar{
    
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, G_SCREENWIDTH, 35)];
    toolbar.tintColor = MC_THEME_COLOR;
    toolbar.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(textFieldDone)];
    toolbar.items = @[space, bar];
    return toolbar;
}

-(void)textFieldDone{
    
    [_textField_Num resignFirstResponder];
    [_textField_Price resignFirstResponder];
    
}

-(void)caculateGG{
    HomeBaseModel * HB = [HomeBaseModel sharedHomeBaseModel];
    HomeBaseTabCellModel * SM = HB.selectedModel;
    if (SM) {
        double Price = [_textField_Price.text doubleValue];
        double Num   = [_textField_Num.text   doubleValue];
        double trustMoney = Price*Num;
        double guaranteeMoney = Price*Num/_lever;
        _lab_trustMoney.text = [NSString stringWithFormat:@"%@ %@",[MCMathUnits stringWithDecimalNumber:trustMoney],SM.quote_symbol];
        _lab_guaranteeMoney.text = [NSString stringWithFormat:@"%@ %@",[MCMathUnits stringWithDecimalNumber:guaranteeMoney],SM.quote_symbol];
    }
}

-(void)textFieldDidChange:(UITextField *)textField{
    textField.text= [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];

    if (textField==_textField_Price) {
        [self caculateGG];
        
    }else if(textField==_textField_Num){
        [self caculateGG];
    }
    
    
}




@end
